package com.ting.test.algorithms.二叉树;

import java.util.LinkedList;
import java.util.Queue;

public class 层序遍历 {
    public static void main(String[] args) {
        cengxu(new TreeNode(3));
    }


    /**
     * 利用队列先进先出的特点
     * @param head
     */
    private static void cengxu(TreeNode head) {
        if (head == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(head);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            System.out.println(poll.val);
            queue.add(poll.left);
            queue.add(poll.right);

        }

    }
}
